﻿#include <iostream>
using namespace std;


struct ListNode 
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{
public:
    // LCR 022. 环形链表 II
    ListNode* detectCycle(ListNode* head)
    {
        ListNode* slow = head, * fast = head;
        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast)
            {
                slow = head;
                while (slow != fast)
                {
                    slow = slow->next;
                    fast = fast->next;
                }
                return slow;
            }
        }
        return nullptr;
    }

    // 19. 删除链表的倒数第 N 个结点
    ListNode* removeNthFromEnd(ListNode* head, int n)
    {
        ListNode* slow = head, * fast = head;
        while (n > 0)
        {
            fast = fast->next;
            n--;
        }

        if (fast == nullptr)
            return head->next;

        while (fast->next != nullptr)
        {
            slow = slow->next;
            fast = fast->next;
        }
        slow->next = slow->next->next;
        return head;
    }
};

int main()
{

    return 0;
}
